package com.quiz.bis.controller;

import com.quiz.bis.domain.model.ExamMonitoringLog;
import com.quiz.bis.service.IExamMonitoringService;
import com.quiz.common.core.controller.BaseController;
import com.quiz.common.core.domain.R;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 监考记录控制器
 * @author ahuan
 */
@RestController
@RequestMapping("/monitoring")
@Tag(name = "监考记录管理")
@RequiredArgsConstructor
public class ExamMonitoringController extends BaseController {

    private final IExamMonitoringService monitoringService;

    /**
     * 记录监考事件
     * @param log 监考记录
     * @return {@link R}<{@link Long}> 记录ID
     */
    @PostMapping("/log")
    public R<Long> logEvent(@RequestBody ExamMonitoringLog log) {
        return monitoringService.logMonitoringEvent(log);
    }

    /**
     * 处理监考事件
     * @param logId 记录ID
     * @param handleResult 处理结果
     * @return {@link R}<{@link Boolean}> 是否处理成功
     */
    @PostMapping("/handle/{logId}")
    public R<Boolean> handleEvent(@PathVariable Long logId, @RequestParam String handleResult) {
        return monitoringService.handleMonitoringEvent(logId, handleResult);
    }

    /**
     * 分页查询监考记录
     *
     * @param groupId 考试组ID
     * @param examId 考试ID（可选）
     * @param sessionId 场次ID（可选）
     * @param userId 用户ID（可选）
     * @param eventType 事件类型（可选）
     * @param status 处理状态（0-未处理，1-已处理，可选）
     * @param startTime 开始时间（可选）
     * @param endTime 结束时间（可选）
     * @param page 分页参数
     * @return {@link TableDataInfo}<{@link ExamMonitoringLog}> 监考记录分页数据
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询监考记录")
    public TableDataInfo<ExamMonitoringLog> page(
            @Parameter(description = "考试组ID", required = true) @RequestParam Long groupId,
            @Parameter(description = "考试ID") @RequestParam(required = false) Long examId,
            @Parameter(description = "场次ID") @RequestParam(required = false) Long sessionId,
            @Parameter(description = "用户ID") @RequestParam(required = false) Long userId,
            @Parameter(description = "事件类型(1-切屏警告,2-强制交卷,3-异常行为)") @RequestParam(required = false) Integer eventType,
            @Parameter(description = "处理状态(0-未处理,1-已处理)") @RequestParam(required = false) Integer status,
            @Parameter(description = "开始时间(yyyy-MM-dd HH:mm:ss)") @RequestParam(required = false) String startTime,
            @Parameter(description = "结束时间(yyyy-MM-dd HH:mm:ss)") @RequestParam(required = false) String endTime,
            PageQuery page) {

        return TableDataInfo.build(monitoringService.selectPage(
                groupId, examId, sessionId, userId,
                eventType, status, startTime, endTime, page));
    }
}